/*Lock and CV test with signal threads*/


#include "syscall.h"
#include "user.h"

#define MAXLOCKNUM 1024

int lock_t2_1, lock_t3, lock_t4, lock_t5, lock_t9, lock_t10;
int cv_t6, cv_t8, cv_t9, cv_t10;


/*test lock entries out of range*/
/*
test1(){
	int i = 0, j =0;
	int l[MAXLOCKNUM];

	for(;i<=MAXLOCKNUM;i++){
		l[i] = CreateLock("weiqiaosong",sizeof("weiqiaosong"));
	}
	Printf("%d number of locks are created.\n", i);
	

	for(j=0;j<=i;j++){
		DestroyLock(l[i]);
	}
	Printf("Extra number of locks are deleted\n");
}

/*
test2a(){
	lock_t2_2 = CreateLock("test2a",sizeof("test2a"));
}

test2b(){
	DestroyLock(lock_t2_2);
}

*/

/*
test7(){
	int i = 0, j =0;
	int cv[MAXLOCKNUM];

	for(;i<=MAXLOCKNUM;i++){
		cv[i] = CreateCondition("weiqiaosong",sizeof("weiqiaosong"));
	}
	Printf("%d number of condition variables are created.\n", i);
	

	for(j=0;j<=i;j++){
		DestroyCondition(cv[i]);
	}
	Printf("Extra number of condition variables are deleted\n");
}
*/
main()
{	
	/*Test for yield*/
    Yield();
  
	Printf("\ntest1 : Createlock test\n");  
	/*wrong length of parameter*/
	Printf("Excepted: CreateLock Error: Bad pointer passed to Create\n");
	CreateLock("weiqiaosong", sizeof("weiqiaosong")+1);
	CreateLock("weiqiaosong", -1);
	CreateLock("weiqiaosong", 0);
	/*lock entries out range*/
	
	
	Printf("\ntest2 : Destroylock test\n");  
	/*destroy wrong index range Lock*/
	Printf("Expected: DestroyLock Error: invalid index\n");
	DestroyLock(-1);	
	DestroyLock(MAXLOCKNUM+1);
	

	
	lock_t2_1 = CreateLock("test2L1",sizeof("test2L1"));
	AcquireLock(lock_t2_1);
	/*destroy in used lock*/
	Printf("Excepted: Fatal DestroyLock Error: The lock you are trying to destroy is in USE!\n");
	DestroyLock(lock_t2_1);

	
	ReleaseLock(lock_t2_1);
	DestroyLock(lock_t2_1);
	/*destroylock twice*/
	/*destroy unexist lock*/
	Printf("Excepted: DestroyLock Error: The lock has been destroyed!\n");
	DestroyLock(lock_t2_1);	
	
	
	Printf("\ntest3 : Acquirelock test\n");  
	/*AcquireLock in wrong index range*/
	Printf("Excepted: AcquireLock Error: invalid index!\n");
	AcquireLock(-1);	
	AcquireLock(MAXLOCKNUM+1);

	lock_t3 = CreateLock("test3L", sizeof("test3L"));
	DestroyLock(lock_t3);
	/*Acquire a deleted lock*/
	Printf("Excepted: AcquireLock Error: You are trying to acquire a lock that has not been created!\n");
	AcquireLock(lock_t3);
	

	
	Printf("\ntest4 : Releaselock test\n");  
	/*ReleaseLock in wrong index range*/
	Printf("Excepted: ReleaseLock Error: invalid index!\n");
	ReleaseLock(-1);	
	ReleaseLock(MAXLOCKNUM+1);
	
	lock_t4 = CreateLock("test4L",sizeof("test4L"));
	DestroyLock(lock_t4);
	/*Release a deleted lock*/
	Printf("Expected: ReleaseLock Error: You are trying to release a lock that has not been created!\n");
	ReleaseLock(lock_t4);
	
	
	Printf("\ntest5 : Normal Lock test\n");  
	lock_t5 = CreateLock("test5L",sizeof("test5L"));
	AcquireLock(lock_t5);
	ReleaseLock(lock_t5);
	DestroyLock(lock_t5);

	
/*Test Condition Variable*/
	Printf("\ntest6: Condition Variable normal test\n");
	cv_t6 = CreateCondition("test6CV",sizeof("test6CV"));
	DestroyCondition(cv_t6);
	
	Printf("\ntest7: CreateCondition test\n");
	
	/*wrong length parameter condition variable*/
	Printf("Expected: CreateCondition Error: Bad pointer passed to Create\n");
	CreateCondition("weiqiaosong",sizeof("Weiqiaosong")+1);
	CreateCondition("a",0);
	CreateCondition("a",-1);
	
	/*create too many condition variable
	*test7();
	*/
	
	Printf("\ntest8: DestroyCondition test\n");
	cv_t8 = CreateCondition("test8CV",sizeof("test8CV"));
	DestroyCondition(cv_t8);
	/*destroy unexist cv*/
	Printf("Expected: DestroyCondition Error: The condition has been destroyed!\n");
	DestroyCondition(cv_t8);
	
	/*destroy wrong index cv*/
	Printf("Excepted: DestroyCondition Error: invalid index!\n");
	DestroyCondition(MAXLOCKNUM+1);
	
	/*destroy in use cv
	*uncomplete, need multi-thread to support
	*/
	
	Printf("\ntest9: WaitCV test\n");
	/*Wait CV of other process
	*uncomplete, need multi-process to support 
	*/
	/*Wait on deleted or unexist CV*/
	cv_t9 = CreateCondition("test9CV",sizeof("test9CV"));
	DestroyCondition(cv_t9);
	lock_t9 = CreateLock("test9L",sizeof("test9L"));
	AcquireLock(lock_t9);
	
	Printf("Expected: WaitCV Error: You are trying to wait on a CV that has not been created!\n");
	WaitCV(lock_t9,cv_t9);
	ReleaseLock(lock_t9);
	DestroyLock(lock_t9);
	
	cv_t9 = CreateCondition("test9CV",sizeof("test9CV"));
	Printf("Expected: WaitCV Error: You are trying to wait on a lock that has not been created!\n");
	WaitCV(lock_t9,cv_t9);
	
	Printf("\ntest10: SignalCV test\n");
	
	/*Signal CV of other process
	*uncomplete, need multi-process to support	
	*/
	/*Signal deleted or unexist CV*/
	cv_t10 = CreateCondition("test10CV",sizeof("test10CV"));
	DestroyCondition(cv_t10);
	lock_t10 = CreateLock("test10L",sizeof("test10L"));
	AcquireLock(lock_t10);
	Printf("Expected: SignalCV Error: You are trying to Signal on a CV that has not been created!\n");
	SignalCV(lock_t10,cv_t10);
	ReleaseLock(lock_t10);
	DestroyLock(lock_t10);
	
	cv_t10 = CreateCondition("test10CV",sizeof("test10CV"));
	Printf("Excepted: SignalCV Error: You are trying to Signal on a lock that has not been created!\n");
	SignalCV(lock_t10,cv_t10);
	
}
